package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.SuggestionPlace;

import java.util.List;

/*
 *@author zhanglong
 *Email:1269521147@qq.com
 */
public class SuggestionAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private List<SuggestionPlace> mDatas;
    GeocodeSearch geocoderSearch;

//    Handler


    public SuggestionAdapter(Context context, List<SuggestionPlace> data) {
        mDatas = data;
        mInflater = LayoutInflater.from(context);
//        geocoderSearch = new GeocodeSearch(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mDatas == null) {
            return 0;
        }
        return mDatas.size();
    }

    @Override
    public SuggestionPlace getItem(int position) {
        // TODO Auto-generated method stub
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final SuggestionPlace sug = mDatas.get(position);

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listitem_suggestion, null);
            holder.nameText = (TextView) convertView.findViewById(R.id.tv_suggestion_name);
            holder.detailText = (TextView) convertView.findViewById(R.id.tv_suggestion_details);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.nameText.setText(sug.getStyleName());
        holder.detailText.setText(sug.getDetails());
//        loadAddress(holder.detailText, sug.getStyleName().toString());

        // name表示地址，第二个参数表示查询城市，中文或者中文全拼，citycode、adcode
        return convertView;
    }

    class ViewHolder {
        TextView nameText;

        TextView detailText;
    }

    public void loadAddress(final TextView text, final String addressname) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    GeocodeQuery query = new GeocodeQuery(addressname, "");
                    text.setText(geocoderSearch.getFromLocationName(query).get(0).getFormatAddress());
                } catch (AMapException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
